package ns3

import (
	"bytes"
	"log"
	"os/exec"
)

func Ns3Create(dir, nodes, time string) (out bytes.Buffer, err error) {
	return runNs3Cmd(dir, nodes, time, "create")
}

func Ns3Emu(dir, nodes, time string) (out bytes.Buffer, err error) {
	return runNs3Cmd(dir, nodes, time, "emu")
}

func Ns3Destroy(dir, nodes string) (out bytes.Buffer, err error) {
	return runNs3Cmd(dir, nodes, "1", "destroy")
}

/*
*
python3 main.py -n 3 -t 10800 create
python3 main.py -n 3 -t 10800 emu
python3 main.py -n 3 -t 10800 destroy
*/
func runNs3Cmd(dir, nodes, time, subcommand string) (out bytes.Buffer, err error) {
	cmd := exec.Command("python3", "pow.py", "-n", nodes, "-t", time, subcommand)

	var stdout bytes.Buffer
	cmd.Stdout = &stdout
	cmd.Dir = dir
	cmd.Stderr = &out
	err = cmd.Run()
	if err != nil {
		log.Printf("exec command out: %v", stdout.String())
		log.Printf("exec command err: %v", out.String())
	}

	return
}
